-module(board_solver_tests).
-include_lib("eunit/include/eunit.hrl").

consecutive_when_passed_a_matching_square_it_should_increment_the_count_of_consecutives_test_() ->
    ?_assertEqual({a, 4}, board_solver:consecutive({a, 3}, a)).

consecutive_when_passed_a_non_matching_square_it_should_return_a_count_of_1_test_() ->
    ?_assertEqual({b, 1}, board_solver:consecutive({a, 3}, b)).

square_result_when_passed_a_matching_square_matching_horizontally_vertically_and_diagonally_it_should_return_incremented_values_test_() ->
    ?_assertEqual({no_winner, {a, 3}, {a, 2}, {a, 3, 1}},
		  board_solver:square_result({a, 2}, {a, 1}, [{a, 2, 1}, {a, 1, 1}, {b, 2, 1}], a, no_winner)).

consecutive_diagonals_can_calculate_diagonals_count_for_square_test_() ->
    [?_assertEqual({a, 4, 1},
		  board_solver:consecutive_diagonals([{a, 3, 1}, {a, 30, 30}, {b, 4, 1}], a)),
     ?_assertEqual({a, 1, 3},
		  board_solver:consecutive_diagonals([{b, 3, 1}, {a, 30, 30}, {a, 4, 2}], a)),
     ?_assertEqual({a, 1, 1},
		  board_solver:consecutive_diagonals([{b, 1, 1}, {a, 30, 30}, {b, 2, 8}], a))
    ].

verify_row_should_know_when_a_line_has_a_winner_test_() ->
    [?_assertEqual(
       {a, 
    	{
    	  {b, 2}, 
    	  [{a, 2}, {b, 2}, {b, 3}], 
    	  [{a, 3, 1}, {b, 1, 1}, {b, 2, 2}]
    	 }
       }, board_solver:verify_row(
    	    {a, 1}, 
    	    [{a, 1}, {b, 1}, {b, 2}], 
    	    [{a, 2, 1}, {a, 1, 1}, {b, 1, 2}, {a, 2, 1}, {b, 2, 1}], 
    	    [a, b, b], 3)),
     ?_assertEqual(
	{no_winner, 
	 {
	   {e, 0}, 
	   [{e, 0}, {e, 0}, {e, 0}], 
	   [{e, 0, 0}, {e, 0, 0}, {e, 0, 0}]
	  }
	}, board_solver:verify_row(
	     {e, 0}, 
	     [{e, 0}, {e, 0}, {e, 0}], 
	     [{e, 0, 0}, {e, 0, 0}, {e, 0, 0}, {e, 0, 0}, {e, 0, 0}], 
	     [e, e, e], 3))
    ].
     

has_won_when_passed_a_list_of_counts_where_one_is_on_or_over_the_limit_it_should_return_the_players_name_test_()->
    [?_assertEqual(a, board_solver:has_won(a, [1, 2, 1, 5], 4)),
     ?_assertEqual(a, board_solver:has_won(a, [1, 2, 1, 4], 4))
    ].

has_won_when_passed_a_list_of_counts_all_under_the_limit_it_should_return_no_winner_test_()->
    ?_assertEqual(
       no_winner, board_solver:has_won(a, [1, 2, 1, 3], 4)).


%% TicTacToe tests

tictactoe_when_passed_a_drawn_game_it_should_return_cat_test_() ->
     [?_assertEqual(cat, board_solver:tictactoe([a,b,a,
						 b,a,b,
						 b,a,b])),
      ?_assertEqual(cat, board_solver:tictactoe([a,b,b,
						 b,a,a,
						 b,a,b])),
      ?_assertEqual(cat, board_solver:tictactoe([a,b,a,
						 b,b,a,
						 b,a,b]))].

tictactoe_when_passed_an_unfinished_game_it_should_return_no_winner_test_() ->
    [?_assertEqual(no_winner, board_solver:tictactoe([e,b,a,
						      a,b,b,
						      a,a,b])),
     ?_assertEqual(no_winner, board_solver:tictactoe([a,b,b,
						      b,a,b,
						      a,a,e])),
     ?_assertEqual(no_winner, board_solver:tictactoe([e,e,e,
						      e,a,e,
						      e,e,e]))
    ].

tictactoe_when_passed_a_won_game_it_should_return_the_winner_test_() ->
    [?_assertEqual(a, board_solver:tictactoe([a,a,a,
					      e,a,e,
					      e,e,e])),
     ?_assertEqual(b, board_solver:tictactoe([a,b,a,
					      e,b,e,
					      e,b,e])),
     ?_assertEqual(a, board_solver:tictactoe([a,b,a,
					      e,a,e,
					      e,b,a]))
    ].

board_solver_when_passed_winning_4_in_a_row_board_it_should_detect_the_winner_test_() ->
    [?_assertEqual(b, board_solver:verify_board([
						 [a,a,a,b],
						 [e,a,b,e],
						 [e,b,e,e],
						 [b,e,e,e]], 4)),
     ?_assertEqual(a, board_solver:verify_board([
						 [a,e,a,b],
						 [e,a,b,e],
						 [a,a,a,a],
						 [b,e,e,e]], 4)),
     ?_assertEqual(b, board_solver:verify_board([
						 [a,e,b,b],
						 [e,a,b,e],
						 [a,e,b,a],
						 [b,e,b,e]], 4))
    ].

board_solver_when_passed_drawn_4_in_a_row_board_it_should_detect_the_draw_test_() ->
    [?_assertEqual(cat, board_solver:verify_board([
						   [a,a,b,b],
						   [a,a,b,b],
						   [b,b,a,a],
						   [a,b,a,b]], 4))
    ].

board_solver_when_passed_unfinished_4_in_a_row_board_it_should_return_no_winner_test_() ->
    [?_assertEqual(no_winner, board_solver:verify_board([
						   [a,a,b,b],
						   [a,a,b,b],
						   [b,e,a,a],
						   [a,b,a,b]], 4)),
     ?_assertEqual(no_winner, board_solver:verify_board([
						   [a,a,b,b],
						   [a,a,b,b],
						   [b,b,a,a],
						   [a,b,a,e]], 4)),
     ?_assertEqual(no_winner, board_solver:verify_board([
						   [e,a,b,b],
						   [a,a,b,b],
						   [b,b,a,a],
						   [a,b,a,b]], 4)),
     ?_assertEqual(no_winner, board_solver:verify_board([
						   [a,a,b,b],
						   [a,a,b,e],
						   [b,b,a,a],
						   [a,b,a,b]], 4))
    ].
